package com.dpiinc.TechPage.user.setting;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dpiinc.TechPage.AIWriteApplication;
import com.dpiinc.TechPage.BaseActivity;
import com.dpiinc.TechPage.R;
import com.dpiinc.TechPage.activity.BottomBarActivity;
import com.dpiinc.TechPage.widget.smartrefresh.header.ClassicsHeader;

import java.util.Locale;

import androidx.annotation.Nullable;

public class LanguageActivity extends BaseActivity {

    View language_zh_layout;
    View language_en_layout;
    View language_fr_layout;
    View language_es_layout;
    TextView language_zh_text;
    TextView language_en_text;
    TextView language_fr_text;
    TextView language_es_text;
    String locale;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.setting_language);
        super.onCreate(savedInstanceState);

        setBackWhite();
        setstatusBarColor(R.color.color_00bad2);

        //当前语言显示为绿色
        language_zh_text = findViewById(R.id.language_zh_text);
        language_en_text = findViewById(R.id.language_en_text);
        language_fr_text = findViewById(R.id.language_fr_text);
        language_es_text = findViewById(R.id.language_es_text);
        locale = getResources().getConfiguration().locale.getLanguage();
       /* if("zh".equals(locale)){
            language_zh_text.setTextColor(getResources().getColor(R.color.color_00b29a));
            ImageView language_zh_select = findViewById(R.id.language_zh_select);
            language_zh_select.setVisibility(View.VISIBLE);
        }else*/ if("en".equals(locale)){
            language_en_text.setTextColor(getResources().getColor(R.color.color_00b29a));
            ImageView language_en_select = findViewById(R.id.language_en_select);
            language_en_select.setVisibility(View.VISIBLE);
        }else if("fr".equals(locale)){
            language_fr_text.setTextColor(getResources().getColor(R.color.color_00b29a));
            ImageView language_en_select = findViewById(R.id.language_fr_select);
            language_en_select.setVisibility(View.VISIBLE);
        }else if("es".equals(locale)){
            language_es_text.setTextColor(getResources().getColor(R.color.color_00b29a));
            ImageView language_en_select = findViewById(R.id.language_es_select);
            language_en_select.setVisibility(View.VISIBLE);
        }

        language_zh_layout = findViewById(R.id.language_zh_layout);
 /*       language_zh_layout.setOnClickListener(view -> {
            locale = getResources().getConfiguration().locale.getLanguage();
            if(!"zh".equals(locale)){
                setLocalLanguage("zh");
            }
        });*/

        language_en_layout = findViewById(R.id.language_en_layout);
        language_en_layout.setOnClickListener(view -> {
            locale = getResources().getConfiguration().locale.getLanguage();
            if(!"en".equals(locale)){
                setLocalLanguage("en");
            }
        });
        language_fr_layout = findViewById(R.id.language_fr_layout);
        language_fr_layout.setOnClickListener(view -> {
            locale = getResources().getConfiguration().locale.getLanguage();
            if(!"fr".equals(locale)){
                setLocalLanguage("fr");
            }
        });
        language_es_layout = findViewById(R.id.language_es_layout);
        language_es_layout.setOnClickListener(view -> {
            locale = getResources().getConfiguration().locale.getLanguage();
            if(!"es".equals(locale)){
                setLocalLanguage("es");
            }
        });
    }

    /**
     * 设置本地语言
     * @param lauType
     */
    public void setLocalLanguage(String lauType) {
        this.getSharedPreferences("app_language",Context.MODE_PRIVATE).edit().putString("lauType",lauType).commit();
        Locale myLocale = new Locale(lauType,"");
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        AIWriteApplication.Companion.instance().updateLanguage();
        res.updateConfiguration(conf, dm);
        finish();
        initClassicsHeader();
        Intent intent = new Intent(this, BottomBarActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK );
        startActivity(intent);
    }

    /**
     * 初始化下拉刷新相关的语言
     */
    public void initClassicsHeader(){
        ClassicsHeader.REFRESH_HEADER_PULLING = getResources().getString(R.string.srl_header_pulling);
        ClassicsHeader.REFRESH_HEADER_REFRESHING = getResources().getString(R.string.srl_header_refreshing);
        ClassicsHeader.REFRESH_HEADER_LOADING = getResources().getString(R.string.srl_header_loading);
        ClassicsHeader.REFRESH_HEADER_RELEASE = getResources().getString(R.string.srl_header_release);
        ClassicsHeader.REFRESH_HEADER_FINISH = getResources().getString(R.string.srl_header_finish);
        ClassicsHeader.REFRESH_HEADER_FAILED = getResources().getString(R.string.srl_header_failed);
        ClassicsHeader.REFRESH_HEADER_UPDATE = getResources().getString(R.string.srl_header_update);
        ClassicsHeader.REFRESH_HEADER_SECONDARY = getResources().getString(R.string.srl_header_secondary);
    }
}
